import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Employee;
import model.Fly;

public class TolovlogooController extends Controller
{
	private static final long	serialVersionUID	= -252714932320381339L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		super.doGet(request, response);
	}

	public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		ServletContext sc = getServletContext();
		RequestDispatcher rd = sc.getRequestDispatcher("/main");
		rd.forward(request, response);
	}

	public void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String html = getViewHtml("main/tolovlogoo", "tolovlogoo");
		String header = getViewHtml("main/tolovlogoo", "table_header");
		html = html.replaceAll("\\{table_header\\}", header);
		String rows = setTableRow();
		html = html.replaceAll("\\{table_rows\\}", rows);
		renderView(html, response);
	}

	public String setTableRow()
	{
		String date = "2012/04/";
		String rows = "";
		for (int category = 1; category <= 3; category++)
		{
			rows += "<tr id=\"text\">" + "<td></td>" + "<td colspan=\"33\"><div class=\"category\">" + Employee.getCategoryName(category) + "</div></td>";
			ArrayList<Employee> employees = Employee.getAllEmployeeByNameASC(getSession(), category);
			int count = 1;
			for (Employee employee : employees)
			{
				rows += "<tr id=\"text\">" + "<td>" + (count++) + "</td>" + "<td>" + employee.getName() + "</td>" + "<td>" + employee.getWillFlyTimeOnThisMonth(false, null) + "</td>";
				for (int day = 1; day <= 31; day++)
				{
					String tempDate = date + (((day + "").length() == 1) ? "0" + day : day);
					Fly fly = Fly.getFlyByEmployeeForThisDate(getSession(), employee, tempDate);
					if ( fly != null )
					{
						rows += "<td class=\"boxto\">";
						rows += "<div>" + Fly.getFlyName(getSession(), fly, 2, true) + "</div>";
						rows += "</td>";
					}
					else
					{
						rows += "<td class=\"boxto\"></td>";
					}

				}
				rows += "</tr>";
			}
		}
		return rows;
	}
}